import {messages} from "share/common";
/**
 * Created by zhouli on 18/2/7
 * Email li.zhou@huilianyi.com
 */
import config from 'config';
import httpFetch from 'share/httpFetch';
import errorMessage from 'share/errorMessage';
import valueListService from 'containers/setting/value-list/value-list.service';
import configureStore from 'stores';

export default {
  //获取是否是老公司信息
  getIsOldCompanyValue() {
    return configureStore.store.getState().login.isOldCompany;
  },
  //设置老公司人员信息
  setOldCompanyPersonPositionInfo(positionInfo) {
    if (positionInfo && positionInfo.length) {
      return positionInfo.map(item => {
        item.legalEntityId = item.companyId;
        item.legalEntityName = item.companyName;
        return item
      })
    } else {
      return positionInfo;
    }
  },
  //获取老公司人员信息
  getOldCompanyPersonPositionInfo(positionInfo) {
    if (positionInfo && positionInfo.length) {
      return positionInfo.map(item => {
        item.companyId = item.legalEntityId;
        item.companyName = item.legalEntityName;
        return item
      })
    } else {
      return positionInfo;
    }
  },
  //单独用来获取证件类型
  getSystemValueList: function (code) {
    let url = '/api/custom/enumerations/template/by/type?type=';
    return httpFetch.get(`${config.baseUrl}${url}${code}`).then(res => {
      return new Promise((resolve) => {
        if (res.data.splice) {
          let result = JSON.parse(JSON.stringify(res));
          result.data = {values: res.data};
          resolve(result);
        } else {
          resolve(res);
        }
      });
    });
  },
  //撤销离职
  cancelResign: function (userOID) {
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/api/users/set/cancel/leaved/' + userOID)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },
  //设置离职时间：点击离职：都是一个接口
  setResignDate: function (userOID, date) {
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/api/users/' + userOID + '/set/leaving/date/' + date)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //重新入职
  rehire: function (userOID) {
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/api/refactor/users/rehire/' + userOID)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //获取可用的电话前缀列表
  getMobilevalidateList() {
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/mobilevalidate/list?isEnabled=false')
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  // 新增个人基本信息
  createPersonDetail(obj) {
    if (this.getIsOldCompanyValue()) {
      obj.userJobsDTOs = this.setOldCompanyPersonPositionInfo(obj.userJobsDTOs)
    }
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/api/refactor/users/v2', obj)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },
  //获取初始化人员信息扩展字段结构
  getInitExtendField(params) {
    // params = {
    //   formOID:"profile中获取扩展字段表单的oid"
    // }
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/custom/values/form', params)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },
  //获取公司对应的profile
  getUserCompanyProfile(companyOID) {
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/function/profiles/company/by/' + companyOID)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },
  //获取个人基本信息
  getPersonDetail(userOID) {
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/users/v2/' + userOID)
        .then((res) => {
          if (this.getIsOldCompanyValue()) {
            res.data.userJobsDTOs = this.getOldCompanyPersonPositionInfo(res.data.userJobsDTOs)
          }
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },
  //更新个人基本信息
  updatePersonDetail(obj) {
    if (this.getIsOldCompanyValue()) {
      obj.userJobsDTOs = this.setOldCompanyPersonPositionInfo(obj.userJobsDTOs)
    }
    return new Promise((resolve, reject) => {
      httpFetch.put(config.baseUrl + '/api/refactor/users/v2', obj)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },
  //获取值列表：根据值列表oid
  //在扩展字段信息里面，有些字段是值列表，这个时候需要从dataSource解析值列表oid获取值列表
  getListByCustomEnumerationOID(customEnumerationOID) {
    return new Promise((resolve, reject) => {
      valueListService.getValueListInfo(customEnumerationOID)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //获取银行卡:马磊完成：返回禁用与禁用的银行卡
  getBankCards(userOID) {
    let param = {
      userOID: userOID,
      enable: null,//新加参数enable ，启用 true，禁用 false，全部 空
    }
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/contact/bank/account', param)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //创建银行卡
  creatBankCard(card) {
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/api/contact/bank/account', card)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },
  //更新银行卡
  updateBankCard(card) {
    return new Promise((resolve, reject) => {
      httpFetch.put(config.baseUrl + '/api/contact/bank/account', card)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //获取证件信息:马磊完成：返回禁用与禁用的证件
  getContactCards(userOID) {
    let param = {
      userOID: userOID,
      enable: null,//新加参数enable ，启用 true，禁用 false，全部 空
    }
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/contact/cards', param)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //创建证件
  creatContactCard(card) {
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/api/contact/cards', card)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },
  //更新证件
  updateContactCard(card) {
    return new Promise((resolve, reject) => {
      httpFetch.put(config.baseUrl + '/api/contact/cards', card)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //获取供应商信息
  getSupplierInfo: function (userOID) {
    let param = {
      userOID: userOID,
      enable: null,//新加参数enable ，启用 true，禁用 false，全部 空
    }
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/contact/supplier/ctrip', param)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //更新携程子账户
  updateSupplierInfo: function (data) {
    // let data = {
    //   userOID: "",
    //   enable: true,
    //   subAccountName: "testtest",
    //   userOID: "3caee0f8-7424-4a21-acc5-f8f3a2910d14",
    //   ...
    // }
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/api/contact/supplier/ctrip?userOID=' + data.userOID, data)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  getCardTypes() {
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/custom/enumeration/system/by/type?systemCustomEnumerationType=1017')
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  /*
  * @return [VendorInfo,FanJia,KongGang,YouXing]
  *   let res = [
      {
        key: "VendorInfo",
        name: ""
      },
      {
        key: "FanJia",
        name: ""
      },
      {
        key: "KongGang",
        name: ""
      },
      {
        key: "YouXing",
        name: ""
      },
    ];
  * */

  getRenderList(userCompanyProfile) {
    let res = [];
    //在用户的默认公司的profile去匹配
    let matchList = [
      {
        renderKey: "VendorInfo",//匹配到就返回的 key
        name: messages('enterprise-manage.key391')/*携程*/,
        matchKey: "vendor",//匹配 profile 的 key
        list: [1002, 10011, 10012, 1010, 10013, 10016],//匹配到任意一个，就成功
      },
      {
        renderKey: "FanJia",
        name: messages('enterprise-manage.key392')/*泛嘉国际*/,
        matchKey: "thridParties",
        list: ["vendor.Fanjia", "vendor.FanjiaHotel", "vendor.FanjiaTrain"]
      },
      {
        renderKey: "KongGang",
        name: messages('enterprise-manage.key393')/*空港嘉华*/,
        matchKey: "thridParties",
        list: ["vendor.KgjhAir"]
      },
      {
        renderKey: "YouXing",
        name: messages('enterprise-manage.key394')/*优行商旅*/,
        matchKey: "thridParties",
        list: ["vendor.UBtripAir"]
      }
    ];
    matchList.map(item => {
      if (this.matchProfile(item, userCompanyProfile)) {
        let obj = {
          key: item.renderKey,
          name: item.name
        };
        res.push(obj);
      }
    });
    /*
    * 说是消费商团队有个东西没有做，前端隐藏
    * 之后开发好了，可以放开
    * todo
    * */
    // 待去掉注释 ----- start
    res = res.filter(item=>item.key === 'VendorInfo');
    // 待去掉注释 ----- end
    return res;
  },
  matchProfile(item, profile) {
    let matchArr = [];
    let list = item.list;
    matchArr = profile[item.matchKey] ? profile[item.matchKey] : [];
    if (item.matchKey !== "vendor") {
      matchArr = matchArr.vendor ? matchArr.vendor : [];
      let re = matchArr.map(item => {
        return item.messageKey;
      });
      matchArr = re;
    }
    //求数组交集 list matchArr
    let res = list.filter(item => matchArr.indexOf(item) > -1);
    return res.length > 0;
  }

}
